module.exports = function ($scope, $state, $resource, $modal, toaster) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
	};
	$scope.currentPage = 1;		//当前页码
	$scope.itemsPerPage = 10;	//每页显示几条
	$scope.pageInfo = {};
	//卡列表
	$scope.loadlist = function () {
		var para = {
			'pageNo': $scope.currentPage,
			'pageSize': $scope.itemsPerPage
		}
		angular.extend(para, $scope.pageInfo);
		$resource('/api/as/ggc/activity/list', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				$scope.activityList = res.data.results;
				$scope.totalItems = res.data.totalRecord;
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

    $scope.loadlist();
    
    $scope.create = function () {
        $state.go('app.tourActivityEdit', { 'type': '0' })
    }
    
    $scope.edit = function (activity) {
        $state.go('app.tourActivityEdit', { 'type': '1', id: activity.id})
    }

	//房间信息
	$scope.info = function (activity) {
		var modalInstance = $modal.open({
			template: require('../views/tourActivityInfo.html'),
			controller: 'tourActivityInfo',
			size: 'lg',
			resolve: {
				activity: function () {
					return activity;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
	}

	$scope.showInWx = function(activity, show){
		var para = {
			'id': activity.id,
			'is_show': show
		}
		$resource('/api/as/ggc/activity/setActivityShow', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				activity.is_show = show;
				toaster.success({ title: '', body: '成功'});
			} else {
				toaster.error({ title: '', body: '失败'});
			}
		});
	}

	$scope.setTPSend = function(activity, flag){
		var para = {
			'id': activity.id,
			'tp_grant': flag
		}
		$resource('/api/as/ggc/activity/setActivityTP', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				activity.tp_grant = flag;
				toaster.success({ title: '', body: '成功'});
			} else {
				toaster.error({ title: '', body: '失败'});
			}
		});
	}

	
	$scope.downloadExl2 = function(exportList,title){
		console.log(exportList);
        const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };  //这里的数据是用来定义导出的格式类型
        const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
        wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(exportList);
        var blob = new Blob([str2binary(XLSX.write(wb, wopts))], {type: 'application/octet-stream' });

        var tmpa = document.createElement("a");
        tmpa.download = title + '.xlsx';
        tmpa.href = URL.createObjectURL(blob); //绑定a标签
        tmpa.click(); //模拟点击实现下载
        setTimeout(function () { //延时释放
            URL.revokeObjectURL(blob); //用URL.revokeObjectURL()来释放这个object URL
        }, 100);
    }
	//导出excel前的二进制转码
	function str2binary(s) {
        if (typeof ArrayBuffer !== 'undefined') {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        } else {
            var buf = new Array(s.length);
            for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        }
	}
	$scope.export = function(activity){
		var dict = {
			'a0': '未支付',
			'a1': '已支付',
			'a2': '已退款'
		}
		$resource('/api/as/ggc/order/getActivityOrderList', {}, {}).save({'id': activity.id}, function (res) {
			if (res.errcode === 0) {
				var exportList = [];
				angular.forEach(res.data, function(item){
					var order = {};
					order['产品名称'] = item.title;
					order['订单编号'] = item.order_code;
					order['姓名'] = item.name;
					order['手机号'] = item.mobile;
					order['身份证号'] = item.cardno;
					order['下单时间'] = item.order_create;
					order['支付状态'] = dict['a'+item.pay_state];
					order['支付金额'] = item.pay_fee;
					order['票状态'] = item.ticket_state=='2'?'出票成功':'未出票';
					order['票唯一码'] = item.ticket_code;
                    exportList.push(order);
                });
                $scope.downloadExl2(exportList, activity.title);
			} else {
				toaster.error({ title: '', body: '无订单信息'});
			}
		});
	}

	//删除卡
	$scope.delete = function(activity){
		if(confirm('确认删除此公告吗？') == true){
			$resource('/api/as/ggc/activity/delete', {}, {}).save(activity, function (res) {
				if (res.errcode === 0){
					toaster.success({title: '', body: '删除成功'});
					$scope.loadlist();
				} else {
					toaster.error({title: '', body: res.errmsg});
				}
			});
		}
  }
  // 设置海报
  $scope.setImage = function(bean){
    console.log(bean);
    var modalInstance = $modal.open({
			template: require('../views/tourPosterList.html'),
			controller: 'tourPosterList',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
  }

};